package com.dkm.orproject.dkmoa.action.flow.overtime;

import com.alibaba.fastjson.JSON;
import com.dkm.orproject.dkmoa.domain.OaBatchOvertimeFlow;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.domain.OaRegularFlow;
import com.dkm.orproject.dkmoa.service.flow.OaRegularFlowService;
import com.dkm.orproject.dkmoa.service.flow.overtime.OaBatchOvertimeFlowService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.Tools;
import io.swagger.annotations.*;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName : BatchOvertimeFlowAction  //类名
 * @Description : 加班流程Action  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-11-03 09:29  //时间
 */
@RestController
@RequestMapping("OaBatchOvertimeFlowAction")
@Api(tags = "加班流程")
public class OaBatchOvertimeFlowAction {

    /**
     * 加班流程Service
     */
    @Resource
    OaBatchOvertimeFlowService batchOvertimeFlowService;

    /**
     * 工具类
     */
    @Resource
    Tools tools;


    /**
     * 回调值
     */
    int row=0;

    /**
     * 日志
     */
    OaLog log;

    /**
     * 登陆人
     */
    OaPersonnel personnel;

    /**
     * 回调map
     */
    Map<String,Object> map;

    /**
     * 分页查询
     * @param batchOvertimeName 加班流程模型 用于条件查询
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 当前页数
     * @param limit 显示的行数
     * @return 加班流程集
     */
    @ApiOperation(value = "员工离职分页查询接口", notes = "加班流程分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "batchOvertimeName",value = "加班流程名称（模糊）",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "显示行数", required = true, dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = OaRegularFlow.class),
    })
    @UserLoginToken
    @GetMapping("findAll")
    public Map<String,Object> findAll(String batchOvertimeName,
            String startTime,
            String endTime,
            Integer page,
            Integer limit,
            HttpServletRequest request){
        try{
            //加班流程分页查询
            List<OaBatchOvertimeFlow> dataDictionaries=batchOvertimeFlowService.findAll(batchOvertimeName,startTime,endTime,page,limit);
            //总行数查询
            Long rowCount=batchOvertimeFlowService.rowCount(batchOvertimeName,startTime,endTime);
            //格式化查询数据
            return Tools.maps(dataDictionaries,rowCount);
        }catch (Exception e){
            //收集错误信息
            addErrorLog(request,"加班流程分页查询",e);
        }
        return null;

    }

    /**
     * id查询
     * @param id 加班流程主键
     * @return 加班流程模型
     */
    @ApiOperation(value = "id查询", notes = "id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "加班流程id",dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = OaRegularFlow.class),
    })
    @UserLoginToken
    @GetMapping("findById/{id}")
    public OaBatchOvertimeFlow findById(@PathVariable("id") Long id, HttpServletRequest request){
        try{
            //id查询
            return batchOvertimeFlowService.findById(id);
        }catch (Exception e){
            //收集错误信息
            addErrorLog(request,"加班流程id查询",e);
        }
        return null;
    }

    /**
     * 报错日志新增
     * @param request 请求 用于得到当前登录人和ip地址
     * @param title 模块标题
     * @param e 异常参数
     */
    private void addErrorLog(HttpServletRequest request,String title,Exception e){
        //答应日志信息
        e.printStackTrace();
        //获取当前用户信息
        personnel= (OaPersonnel) request.getSession().getAttribute("user");
        //新建日志
        log=new OaLog();
        //日志类型
        log.setLogType("0");
        //日志内容
        log.setLogBody(e.getLocalizedMessage());
        //日志标题
        log.setLogTitle(title);
        //当前请求ip地址
        log.setLogIp(request.getRemoteAddr());
        //判断当前操作人是否存在
        if(ObjectUtils.isEmpty(personnel)){
            //操作人
            log.setCreatedBy(personnel.getPersonnelName());
        }
        //新增报错信息
        tools.errorMap(log);
    }

    /**
     * 起草人节点
     * @param batchOvertimeFlow 加班流程模型
     * @param request 服务器请求
     * @return 回调函数
     */
    @ApiOperation(value = "起草人节点", notes = "起草人节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "batchOvertimeDetailStart",value = "加班开始时间",dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "batchOvertimeDetailEnd",value = "加班结束时间",dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "batchOvertimeDetailHour",value = "小时数",dataType = "decimal",paramType = "query"),
            @ApiImplicitParam(name = "batchOvertimeDetailCause",value = "加班事由",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "personnelId",value = "申请人id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("apply")
    public Map<String,Object> add(OaBatchOvertimeFlow batchOvertimeFlow, HttpServletRequest request){
        //获取当前操作人信息
        rowMap(request);
        //判断登录人是否存在
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            //加班流程新增
            row=batchOvertimeFlowService.add(batchOvertimeFlow,personnel);
            //日志标题
            log.setLogTitle("加班流程新增");
            //日志类型
            log.setLogType("1");
            //日志内容
            log.setLogBody(JSON.toJSON(batchOvertimeFlow).toString());
            //新增日志信息 格式化返回信息
            tools.rowMap(map,row,log);
        }catch (Exception e){
            //收集报错信息
            addErrorLog(request,"加班流程新增",e);
        }
        return map;
    }

    /**
     * 部门领导通过
     * @param request 服务器请求
     * @param departmentOpinion 审核信息
     * @return 回调函数
     */
    @ApiOperation(value = "部门领导通过", notes = "部门领导通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkBatchOvertimeId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "regularDepartmentOpinion",value = "部门意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("sectionPass")
    public Map<String,Object> sectionPass(HttpServletRequest request,Long pkBatchOvertimeId,String departmentOpinion){
        //获取当前操作人
        rowMap(request);
        //判断当前操作人是否存在
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            //部门领导通过
            row=batchOvertimeFlowService.sectionPass(departmentOpinion,personnel,pkBatchOvertimeId);
            //日志类型
            log.setLogType("1");
            //日志内容
            log.setLogBody(departmentOpinion);
            //新增日志 格式化返回信息
            tools.rowMap(map,row,log);
        }catch (Exception e){
            addErrorLog(request,"部门审核通过",e);
        }
        return map;
    }

    /**
     * 部门领导不通过
     * @param request 服务器请求
     * @param pkBatchOvertimeId 审核信息
     * @return 回调函数
     */
    @ApiOperation(value = "部门领导不通过", notes = "部门领导不通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkBatchOvertimeId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "personnelId",value = "转正人员id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "formReason",value = "不同意意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("sectionNoPass")
    public Map<String,Object> sectionNoPass(HttpServletRequest request,Long pkBatchOvertimeId,String formReason){
        //获取当前操作人
        rowMap(request);
        //判断当前操作人是否存在
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            //部门领导不通过
            row=batchOvertimeFlowService.sectionNoPass(pkBatchOvertimeId,personnel,formReason);
            //日志标题
            log.setLogTitle("部门审核不通过");
            //日志类型
            log.setLogType("1");
            //日志内容
            log.setLogBody(formReason);
            //新增日志 格式化返回参数
            tools.rowMap(map,row,log);
        }catch (Exception e){
            //收集报错信息
            addErrorLog(request,"部门审核不通过",e);
        }
        return map;
    }

    /**
     * 人事审核通过
     * @param request 服务器请求
     * @param personnelOpinion 人事审核信息
     * @return 回调函数
     */
    @ApiOperation(value = "人事审核通过", notes = "人事审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkBatchOvertimeId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "personnelOpinion",value = "人事通过意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("staffingPass")
    public Map<String,Object> staffingPass(HttpServletRequest request,Long pkBatchOvertimeId,String personnelOpinion){
        //获取当前操作人
        rowMap(request);
        //判断当前操作人是否存在
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            //人事审核通过
            row=batchOvertimeFlowService.staffingPass(pkBatchOvertimeId,personnelOpinion,personnel);
            //日志标题
            log.setLogTitle("人事审核通过");
            //日志类型
            log.setLogType("1");
            //日志内容
            log.setLogBody(personnelOpinion);
            //记录日志 格式化返回参数
            tools.rowMap(map,row,log);
        }catch (Exception e){
            //收集错误信息
            addErrorLog(request,"人事审核通过",e);
        }
        return map;
    }



    /**
     * 总经理审核通过
     * @param request 服务器请求
     * @param managerOpinion 总经理审核信息
     * @return 回调函数
     */
    @ApiOperation(value = "总经理审核通过", notes = "总经理审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkBatchOvertimeId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "managerOpinion",value = "总经理审核信息",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("exPass")
    public Map<String,Object> exPass(HttpServletRequest request,Long pkBatchOvertimeId,String managerOpinion){
        //获取当前操作人
        rowMap(request);
        //判断当前操作人是否存在
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            //总经理通过
            row=batchOvertimeFlowService.exPass(pkBatchOvertimeId,managerOpinion,personnel);
            //日志标题
            log.setLogTitle("总经理审核通过");
            //日志类型
            log.setLogType("1");
            //日志内容
            log.setLogBody(managerOpinion);
            //日志信息 格式化返回参数
            tools.rowMap(map,row,log);
        }catch (Exception e){
            //收集报错信息
            addErrorLog(request,"总经理审核通过",e);
        }
        return map;
    }

    /**
     * 总经理审核不通过
     * @param request 服务器请求
     * @param managerOpinion 总经理审核信息
     * @return 回调函数
     */
    @ApiOperation(value = "总经理审核不通过", notes = "总经理审核不通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkBatchOvertimeId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "personnelOpinion",value = "总经理审核信息",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("exNoPass")
    public Map<String,Object> exNoPass(HttpServletRequest request,Long pkBatchOvertimeId,String managerOpinion){
        //获取当前操作人
        rowMap(request);
        //判断当前操作人是否存在
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            //总经理审核不通过
            row=batchOvertimeFlowService.exNoPass(pkBatchOvertimeId,managerOpinion,personnel);
            //日志标题
            log.setLogTitle("总经理审核不通过");
            //日志类型
            log.setLogType("1");
            //日志内容
            log.setLogBody(managerOpinion);
            //记录日志 格式化返回参数
            tools.rowMap(map,row,log);
        }catch (Exception e){
            addErrorLog(request,"总经理审核不通过",e);
        }
        return map;
    }

    /**
     * 人事审核不通过
     * @param request 服务器请求
     * @param pkBatchOvertimeId 人事审核信息
     * @param formReason 不通过原因
     * @return 回调函数
     */
    @ApiOperation(value = "人事审核不通过", notes = "人事审核不通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkBatchOvertimeId",value = "加班流程id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "formReason",value = "不同意意见",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("staffingNoPass")
    public Map<String,Object> staffingNoPass(HttpServletRequest request,Long pkBatchOvertimeId,String formReason){
        rowMap(request);
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            row=batchOvertimeFlowService.staffingNoPass(pkBatchOvertimeId,personnel,formReason);
            log.setLogTitle("人事审核不通过");
            log.setLogType("1");
            log.setLogBody(formReason);
            tools.rowMap(map,row,log);
        }catch (Exception e){
            addErrorLog(request,"人事审核不通过",e);
        }
        return map;
    }

    /**
     * 批量删除
     * @param ids 加班流程id集
     * @param request 服务器请求
     * @return 回调函数
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "加班流程批量id",dataType = "Long[]",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @DeleteMapping("deleteAll")
    public Map<String,Object> deleteAll(@RequestParam(value = "ids[]",required = false) Long[] ids,HttpServletRequest request){
        rowMap(request);
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            List<Long> list= Arrays.asList(ids);
            List<OaRegularFlow> dataDictionaries=batchOvertimeFlowService.selectAll(list);
            row=batchOvertimeFlowService.deleteAll(list);
            log.setLogTitle("加班流程批量删除");
            log.setLogBody(JSON.toJSON(dataDictionaries).toString());
            log.setLogType("1");
            tools.rowMap(map,row,log);
        }catch (Exception e){
            addErrorLog(request,"加班流程批量删除",e);
        }
        return map;
    }

    /**
     * id删除
     * @param id 加班流程id
     * @param request 服务器请求
     * @return 回调函数
     */
    @ApiOperation(value = "id删除", notes = "id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "加班流程id",dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除成功"),
    })
    @UserLoginToken
    @DeleteMapping("deleteById/{id}")
    public Map<String,Object> deleteById(@PathVariable(value = "id",required = false)Long id,HttpServletRequest request){
        rowMap(request);
        if(ObjectUtils.isEmpty(personnel)){
            return tools.rowMap(map,0,null);
        }
        try{
            OaBatchOvertimeFlow flow=batchOvertimeFlowService.findById(id);
            row=batchOvertimeFlowService.deleteById(id);
            log.setLogTitle("加班流程删除");
            log.setLogBody(JSON.toJSON(flow).toString());
            log.setLogType("1");
            tools.rowMap(map,row,log);
        }catch (Exception e){
            addErrorLog(request,"加班流程删除",e);
        }
        return map;
    }

    /**
     * 优化代码
     * @param request 服务器请求
     */
    private void rowMap(HttpServletRequest request){
        //新建返回Map模型
        map=new HashMap<>(16);
        //获取登陆人信息
        personnel= (OaPersonnel) request.getSession().getAttribute("user");
        //为空则清空map
        if(ObjectUtils.isEmpty(personnel)){
            return;
        }
        //新建日志
        log=new OaLog();
        //得到ip
        log.setLogIp(request.getRemoteAddr());
        //登陆人名称
        log.setCreatedBy(personnel.getPersonnelName());
    }

    /**
     * 日期格式化
     *
     * @param binder 服务器参数转换器
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // true:允许输入空值，false:不能为空值
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


}
